<?php
/**
 * 用户控制器
 * User: mrlifang
 * Date: 17/5/5
 * Time: 下午4:12
 */

namespace Admin\Controller;


use Frame\Libs\BaseController;
use Admin\Model\UserModel;
use Frame\Vendor\Captcha;

class UserController extends BaseController
{
    /*
     * 显示用户列表
     */
    public function index(){
        $this->denyAccess();
        //创建用户模型类对象
        $modelObj = UserModel::getInstance();

        //调用模型类对象的fetchAll()方法返回所有的用户信息
        $users = $modelObj->fetchAll();

        //向视图赋值并显示视图
        $this->smarty->assign("users",$users);
        $this->smarty->display("User".DS."index.html");
    }

    /*
     * 删除用户功能
     */
    public function delete(){
        $this->denyAccess();
        $id = $_GET['id'];

        //创建用户模型类对象
        $modelObj = UserModel::getInstance();
        //调用模型类对象的fetchAll()方法返回所有的用户信息
        if($modelObj->delete($id)){
            $this->jump("id={$id}的用户删除成功","?c=User&a=index");
        }else{
            $this->jump("id={$id}的用户删除失败","?c=User&a=index");
        }
    }

    /*
     * 显示添加用户的表单
     */
    public function add(){
        $this->denyAccess();
        $this->smarty->display("User".DS."add.html");
    }

    /*
     * 插入表单到数据库
     */
    public function insert(){
        $this->denyAccess();
        //获取表单提交值
        //判断用户名是否存在
        $username = $_POST['username'];
        $records = UserModel::getInstance()->rowCount("username="."'$username'");
        if($records){
          $this->jump("用户名{$username}已经被注册了");
        }

        $data['username'] = $_POST['username'];
        if($_POST['password'] != $_POST['confirmpwd']){
            $this->jump("两次输入的密码不一致");
        }
        /*
         *
         * [username] => zhang
           [password] => 11111
           [confirmpwd] => 11111
           [name] => zhang
           [tel] => 13671181498
           [status] => 1
         * */
        $data['password'] = md5($_POST['password']);
        $data['name'] = $_POST['name'];
        $data['tel'] = $_POST['tel'];
        $data['status'] = $_POST['status'];
//        $data['role'] = $_POST['role'];
//        $data['addate'] = $_POST['addate'];

        //调用模型类对象写入方法写入数据

        if (UserModel::getInstance()->insert($data)){
            $this->jump("用户注册成功","?c=User&a=index");
        }else {
            $this->jump("用户注册失败","?c=User&a=add");
        }
        //跳转到列表页
        //
    }

    /*
     * 显示修改用户的视图
     */
    public function edit(){
        $this->denyAccess();
        $id = $_GET['id'];
        $user = UserModel::getInstance()->fetchOne("id = ".$id);
        $this->smarty->assign('user',$user);
        $this->smarty->display("User".DS."edit.html");

    }

    /*
     * 更新用户资料
     */
    public function update(){
        $this->denyAccess();
        //获取表单提交值
        //判断密码是否为空
        if($_POST["password"] == $_POST["confirmpwd"]){
            if(!empty($_POST["password"])){
                $data['password'] = md5($_POST['password']);
                $data['name'] = $_POST['name'];
                $data['tel'] = $_POST['tel'];
                $data['status'] = $_POST['status'];
                $data['id'] = $_POST['id'];

                if(UserModel::getInstance()->exec($data)){
                    $this->jump("修改成功","?c=user&a=index");
                }else {
                    $this->jump("修改失败","?c=user&a=index");
                }

            }

        }
    }
    /*
     * 用户登录的方法
     */
    public function login(){
        //调用登录的视图文件 login.html
        $this->smarty->display("User".DS."login.html");

    }

    /*
     * 用户登录检测逻辑
     * //①获取表单提交值
       //②将用户账户信息,与数据库中的账户信息进行比对
       //③更新用户数据库中的信息
       //④将用户必要信息存入session
       //⑤进入后台管理首页
     */
    public function loginCheck(){

        //(1) 获取表单提交值
        $username = $_POST['username'];
        $password = md5($_POST['password']);
        $verify = $_POST['verify'];
        $data['last_login_ip'] = $_SERVER['REMOTE_ADDR'];
        $data['last_login_time'] = time();

        //(2) 判断验证码输入是否正常 用户控制器UserController
        if(strtolower($verify) != $_SESSION['captcha']){
            $this->jump("验证码格式错误!","?c=User&a=login");
        }


        //(3) 判断用户输入的登录信息是否正确
        $user = UserModel::getInstance()->fetchOne("username = '$username' and password = '$password'");
        if(empty($user)){
            $this->jump("用户名或者密码不正确!","?c=User&a=login");
        }

        //(4)更新用户信息,最后登录的IP 最后登录的时间 登录次数
        UserModel::getInstance()->loginUpdate($data,$user['id']);

        //(5)将用户的必要信息存入SESSION
        $_SESSION['uid'] = $user['id']; //用户id
        $_SESSION['username'] = $user['username']; //用户名称

        //(4)跳转到后台管理首页
        $this->jump("用户登录成功","?c=index");
    }

    /*
     * 生成验证码的方法
     */
    public function captcha(){
        //创建验证码的对象
        $c = new Captcha();
        //获取验证码的值 并且存入SESSION
        $_SESSION['captcha'] = $c->getCode();
    }

    /*
     * 用户退出方法
     */
    public function logout(){
        unset($_SESSION['uid']);
        unset($_SESSION['username']);
        session_destroy();//删除SESSION文件
        $this->jump("用户退出成功~","admin.php?a=User&c=login");
    }


}